﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.QLMS.Entity.SystemSetting;
using ZDSoft.QLMS.Entity;

namespace ZDSoft.QLMS.Component.SystemSettingComponent
{
    public class SystemSettingComponent
    {
        IList<User> userList;
        IList<Role> roleList;
        IList<SystemFunction> systemFunctionList;
        IList<Course> courseList;
        IList<Entity.Type> typeList;
        IList<UserCourse> userCourseList;

        public void InitSystemDate()
        {
            #region 系统基础数据
            InitSystemFunction();
            InitRole();
            InitUser();
            #endregion

            #region 业务基础数据
            InitType();
            InitCourse();
            #endregion

            userCourseList = new List<UserCourse>();
            userCourseList.Add(new UserCourse() { Course = courseList[0], User = userList[1] });
            foreach (UserCourse entity in userCourseList)
            {
                entity.Create();
            }
        }

        #region 系统基础数据
        public void InitUser()
        {
            userList = new List<User>()
            {
                new User(){ Account="Admin", Password="1", IsActive=true, IsReset=false},
                new User(){ Account="CourseCharge", Password="1", IsActive=true, IsReset=false},
                new User(){ Account="All", Password="1", IsActive=true, IsReset=false}
            };

            userList[0].Roles = new List<Role>();
            userList[0].Roles.Add(roleList[0]);
            userList[0].Roles.Add(roleList[1]);
            userList[1].Roles = new List<Role>();
            userList[1].Roles.Add(roleList[0]);
            userList[1].Roles.Add(roleList[2]);
            userList[2].Roles = new List<Role>();
            userList[2].Roles.Add(roleList[0]);
            userList[2].Roles.Add(roleList[1]);
            userList[2].Roles.Add(roleList[2]);

            foreach (User user in userList)
            {
                user.Create();
            }
        }

        public void InitRole()
        {
            roleList = new List<Role>()
            {
                new Role() { RoleName = "基本用户", IsActive = true },
                new Role() { RoleName = "系统管理员", IsActive = true },
                new Role() { RoleName = "课程负责人", IsActive = true }
            };

            roleList[0].SystemFunctions = new List<SystemFunction>();
            roleList[0].SystemFunctions.Add(systemFunctionList[0]);
            foreach (SystemFunction systemFunction in systemFunctionList[0].Childern)
            {
                roleList[0].SystemFunctions.Add(systemFunction);
            }
            roleList[0].SystemFunctions.Add(systemFunctionList[4]);
            foreach (SystemFunction systemFunction in systemFunctionList[4].Childern)
            {
                roleList[0].SystemFunctions.Add(systemFunction);
            }

            roleList[1].SystemFunctions = new List<SystemFunction>();
            roleList[1].SystemFunctions.Add(systemFunctionList[1]);
            foreach (SystemFunction systemFunction in systemFunctionList[1].Childern)
            {
                roleList[1].SystemFunctions.Add(systemFunction);
            }

            roleList[2].SystemFunctions = new List<SystemFunction>();
            roleList[2].SystemFunctions.Add(systemFunctionList[2]);
            foreach (SystemFunction systemFunction in systemFunctionList[2].Childern)
            {
                roleList[2].SystemFunctions.Add(systemFunction);
            }
            roleList[2].SystemFunctions.Add(systemFunctionList[3]);
            foreach (SystemFunction systemFunction in systemFunctionList[3].Childern)
            {
                roleList[2].SystemFunctions.Add(systemFunction);
            }

            foreach (Role role in roleList)
            {
                role.Create();
            }
        }

        public void InitSystemFunction()
        {
            systemFunctionList = new List<SystemFunction>()
            {
                new SystemFunction(){ SystemFunctionName="首页", ClassName="HomeController", ControllerName="Home", ActionName="Index", IsActive=true, Childern=new List<SystemFunction>(){}},

                new SystemFunction(){ SystemFunctionName="系统管理", ControllerName="System", ActionName="", IsActive=true,  Childern=new List<SystemFunction>()
                {
                new SystemFunction(){ SystemFunctionName="用户管理", ControllerName="User", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="角色管理", ControllerName="Role", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="课程管理", ControllerName="Course", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="机构管理", ControllerName="Organization", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() }
                }},

                new SystemFunction(){ SystemFunctionName="题库管理", ControllerName="QuestionLibrary", ActionName="", IsActive=true,  Childern=new List<SystemFunction>()
                {
                new SystemFunction(){ SystemFunctionName="出题任务", ControllerName="SetQuestionTask", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="知识点管理", ControllerName="KnowledgePoint", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="题型管理", ControllerName="Type", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="题目管理", ControllerName="Question", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="题目审核", ControllerName="QuestionExam", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() }
                }},

                new SystemFunction(){ SystemFunctionName="出题管理", ControllerName="TestPaper", ActionName="", IsActive=true,  Childern=new List<SystemFunction>()
                {
                new SystemFunction(){ SystemFunctionName="出题策略管理", ControllerName="Strategy", ActionName="Index", IsActive=true, Childern=new List<SystemFunction>() },
                  new SystemFunction(){ SystemFunctionName="出题策略审核", ControllerName="StrategyExma", ActionName="Index", IsActive=true, Childern=new List<SystemFunction>() },
                new SystemFunction(){ SystemFunctionName="试卷管理", ControllerName="TestPaper", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                 new SystemFunction(){ SystemFunctionName="试卷审核", ControllerName="TestPaperExma", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() },
                 new SystemFunction(){ SystemFunctionName="考试管理", ControllerName="Test", ActionName="Index", IsActive=true,  Childern=new List<SystemFunction>() }
                }},

                new SystemFunction(){ SystemFunctionName="个人管理", ControllerName="Account", ActionName="", IsActive=true,  Childern=new List<SystemFunction>()
                {
                new SystemFunction(){ SystemFunctionName="修改密码", ControllerName="Account", ActionName="UpdatePwd", IsActive=true,  Childern=new List<SystemFunction>() }
                }}
            };
            foreach (SystemFunction function in systemFunctionList)
            {
                if (function.Childern != null)
                {
                    foreach (SystemFunction subFunction in function.Childern)
                    {
                        subFunction.Create();
                    }
                }
                function.Create();
            }
        }
        #endregion

        #region 业务基础数据
        public void InitCourse()
        {
            courseList = new List<Course>()
            {
                new Course(){ Name="C#基础程序", IsActive=true},
                new Course(){ Name="Web", IsActive=true},
                new Course(){ Name="数据结构",IsActive=true},
                new Course(){ Name="dotNet",IsActive=true}
            };
            foreach (Course entity in courseList)
            {
                entity.Create();
            }
        }

        public void InitType()
        {
            typeList = new List<Entity.Type>()
            {
                new Entity.Type(){ Name="单选题",IsActive=true},
                new Entity.Type(){ Name="多选题",IsActive=true },
                new Entity.Type(){ Name="填空题",IsActive=true},
                new Entity.Type(){ Name="简答题",IsActive=true },
                new Entity.Type(){ Name="判断题",IsActive=true },
            };
            foreach (Entity.Type entity in typeList)
            {
                entity.Create();
            }
        }
        #endregion
    }
}